﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;
    using WMS6.Web.UserControls;

    public class BillingDailyInvList : BasePage
    {
        protected Button btnAction;
        protected Button btnExport;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected Button btnRun;
        protected HtmlForm F9231;
        protected GridView gvBillingDailyInv;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblBillingOrderList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2BillingDailyInvEntityList(BillingDailyInvQueryEntity billingDailyInvQueryEntity)
        {
            this.Session["BillingDailyInvQueryEntity"] = billingDailyInvQueryEntity;
            this.PagerFooter.CurrentPage = int.Parse(billingDailyInvQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = BillingServiceClientProxy.GetInstance().GetBillingDailyInvByQueryList(billingDailyInvQueryEntity);
            base.BindData2GridView(this.gvBillingDailyInv, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
            billingDailyInvQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = billingDailyInvQueryEntity.CurrentPage;
            billingDailyInvQueryEntity.CurrentPage = "0";
            this.gvBillingDailyInv.Columns[1].Visible = false;
            this.gvBillingDailyInv.AllowSorting = false;
            this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            this.gvBillingDailyInv.ShowFooter = false;
            this.hdnActionID.Value = "";
            billingDailyInvQueryEntity.IsGetAll = false;
            billingDailyInvQueryEntity.CurrentPage = currentPageIndex;
            this.SetBillingDailyInvQueryEntity(billingDailyInvQueryEntity);
            base.Export2Excel("BillingDailyInv.xls", this.gvBillingDailyInv);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
            billingDailyInvQueryEntity.CurrentPage = "0";
            this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
            billingDailyInvQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("BillingDailyInvQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
            if (int.Parse(billingDailyInvQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                billingDailyInvQueryEntity.CurrentPage = (int.Parse(billingDailyInvQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
            if (int.Parse(billingDailyInvQueryEntity.CurrentPage) > 0)
            {
                billingDailyInvQueryEntity.CurrentPage = (int.Parse(billingDailyInvQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            }
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
            try
            {
                List<string> daysList = new List<string>();
                string day = string.Empty;
                for (int i = 0; i < this.gvBillingDailyInv.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvBillingDailyInv.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        day = this.gvBillingDailyInv.DataKeys[i]["DAYS"].ToString() + ";" + this.gvBillingDailyInv.DataKeys[i]["IS_BILLING_CREDENCE"].ToString() + ";" + this.gvBillingDailyInv.DataKeys[i]["IS_STATEMENT"].ToString();
                        daysList.Add(day);
                    }
                }
                if ((daysList == null) || (daysList.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9230_004");
                }
                else
                {
                    string result = BillingServiceClientProxy.GetInstance().GenerateDailyInvBillingCredence(this.hdnOwnerID.Value, daysList);
                    if (string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_005");
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_006") + result;
                    }
                    BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
                    this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "BillingDailyInvListPage", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private BillingDailyInvQueryEntity GetBillingDailyInvQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as BillingDailyInvQueryEntity);
            }
            return (base.PageQueryEntitySession as BillingDailyInvQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
                billingDailyInvQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            }
        }

        protected void gvBillingDailyInv_Init(object sender, EventArgs e)
        {
        }

        protected void gvBillingDailyInv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
            }
        }

        protected void gvBillingDailyInv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvBillingDailyInv.DataKeys[e.NewSelectedIndex]["DAYS"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvBillingDailyInv_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                BillingDailyInvQueryEntity billingDailyInvQueryEntity = this.GetBillingDailyInvQueryEntity();
                billingDailyInvQueryEntity.SortField = e.SortExpression;
                if (billingDailyInvQueryEntity.SortDirection == "ASC")
                {
                    billingDailyInvQueryEntity.SortDirection = "DESC";
                }
                else if (billingDailyInvQueryEntity.SortDirection == "DESC")
                {
                    billingDailyInvQueryEntity.SortDirection = "ASC";
                }
                this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            BillingDailyInvQueryEntity billingDailyInvQueryEntity = new BillingDailyInvQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    billingDailyInvQueryEntity.OwnerID = this.PreviousPage.Owner;
                    billingDailyInvQueryEntity.BeginDate = this.PreviousPage.BeginDate;
                    billingDailyInvQueryEntity.EndDate = this.PreviousPage.EndDate;
                    this.hdnOwnerID.Value = this.PreviousPage.Owner;
                    this.Session["BillingDailyInvQueryEntity"] = null;
                }
                else
                {
                    billingDailyInvQueryEntity.OwnerID = "";
                    billingDailyInvQueryEntity.BeginDate = "";
                    billingDailyInvQueryEntity.EndDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            billingDailyInvQueryEntity.IsGetAll = false;
            billingDailyInvQueryEntity.CurrentPage = "0";
            billingDailyInvQueryEntity.PageSize = this.gvBillingDailyInv.PageSize.ToString();
            billingDailyInvQueryEntity.SortField = "DAYS";
            billingDailyInvQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvBillingDailyInv.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvBillingDailyInv.Columns[1].Visible = false;
                this.gvBillingDailyInv.Columns[2].Visible = true;
                this.btnRun.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            if (this.Session["BillingDailyInvQueryEntity"] != null)
            {
                billingDailyInvQueryEntity = this.Session["BillingDailyInvQueryEntity"] as BillingDailyInvQueryEntity;
            }
            this.SetBillingDailyInvQueryEntity(billingDailyInvQueryEntity);
            this.BindData2BillingDailyInvEntityList(billingDailyInvQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBillingDailyInvQueryEntity(BillingDailyInvQueryEntity billingDailyInvQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = billingDailyInvQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = billingDailyInvQueryEntity;
            }
        }

        public BillingDailyInvQuery PreviousPage
        {
            get
            {
                return (BillingDailyInvQuery) base.PreviousPage;
            }
        }
    }
}

